<div class="row">
    <div class="col-md-12 widget-module">
        <div class="box-widget">
            <div class="widget-head clearfix">
                <h4 class="pull-left"><?php echo $this->supplier['name']; ?></h4>
            </div>
            
            <div class="widget-container">
                <div class="widget-block">
                    <div class="widget-content">
                        <form id="salesinvoice" method="post" class="form-horizontal form-box">
                            <div id="headtitle" class="form-group">
                                <label class="col-lg-4 control-label" style="text-align: left;"><?php echo $this->translate('product name'); ?></label>
                                <label class="col-lg-1 control-label" style="text-align: left;"><?php echo $this->translate('net'); ?></label>
                                <label class="col-lg-1 control-label" style="text-align: left;"><?php echo $this->translate('brand'); ?></label>
                                <label class="col-lg-2 control-label" style="text-align: left;"><?php echo $this->translate('type'); ?></label>
                                <label class="col-lg-1 control-label" style="text-align: left;"><?php echo $this->translate('price'); ?></label>
                                <label class="col-lg-1 control-label" style="text-align: left;"><?php echo $this->translate('quantity'); ?></label>
                                <label class="col-lg-2 control-label" style="text-align: left;"><?php echo $this->translate('total price'); ?></label>
                            </div>
                            <?php foreach($this->products as $product): ?>
                            <div id="product" class="form-group <?php echo $this->classError[$product->id]; ?>">
                                <label class="col-lg-4 control-label" style="text-align: left; text-transform: capitalize;">
                                    <?php echo $product->name; ?>
                                </label>
                                <label class="col-lg-1 control-label" style="text-align: left;">
                                    <?php echo $product->net; ?>
                                </label>
                                <label class="col-lg-1 control-label" style="text-align: left; text-transform: uppercase;">
                                    <?php echo $product->brand; ?>
                                </label>
                                <label class="col-lg-2 control-label" style="text-align: left; text-transform: uppercase;">
                                    <?php echo $product->type; ?>
                                </label>
                                <div id="price" class="col-lg-1">
                                    <?php
                                        $price = $product->price;
                                    ?>
                                    <label id="text_price[<?php echo $product->id; ?>][price]" style="text-align: left;"><?php echo number_format($price); ?></label>
                                    <a class="btn btn-mini btn-info" onclick="updatePrice(<?php echo $product->id; ?>)">Sửa</a>
                                </div>
                                
                                <div id="quantity" class="col-lg-1">
                                    <input class="form-control" type="text" value="0" id="quantity" />
                                    <input type="hidden" value="0" name="product[<?php echo $product->id; ?>][quantity]" />
                                </div>
                                
                                <div id="total" class="col-lg-2">
                                    <input readonly="" class="form-control" type="text" value="0" class="form-control" id="product[<?php echo $product->id; ?>][total]" />
                                    <input type="hidden" id="total" value="0" name="product[<?php echo $product->id; ?>][total]" />
                                </div>
                                
                                <input type="hidden" name="product[<?php echo $product->id; ?>][price]" id="price" value="<?php echo $price; ?>" />
                                <input type="hidden" name="product[<?php echo $product->id; ?>][id]" id="id" value="<?php echo $product->id; ?>" />
                                <?php //echo $this->messageError[$product['id']]['price']; ?>
                            </div>
                            <?php endforeach; ?>
                            
                            <div class="form-group">
                                <label class="col-lg-9 control-label"><?php echo $this->translate('Ngày'); ?></label>
                                <div class="col-lg-3">
                                    <input id="datepicker" type="text" class="form-control" name="date" value="" />
                                </div>
                            </div>
                            
                            <div class="form-group">
                                <label class="col-lg-9 control-label"><?php echo $this->translate('subtotal'); ?></label>
                                <div class="col-lg-3">
                                    <input readonly="" type="text" class="form-control" id="subtotal" value="0" />
                                    <input type="hidden" name="subtotal" value="0" />
                                </div>
                            </div>
                            
                            <?php
                                $dept = $this->dept;
                                $payment = $this->payment;
                                $order = $this->order;
                                $refund = $this->refund;
                                //$totalDept = $dept->dept + $order->subtotal - $order->totalpay - $payment->total - $refund->total;
                                $totalDept = $this->totalDept;
                            ?>
                            
                            <div class="form-group">
                                <label class="col-lg-9 control-label"><?php echo $this->translate('dept'); ?></label>
                                <div class="col-lg-3">
                                    <input readonly="" type="text" class="form-control" id="dept" value="" />
                                    <input type="hidden" name="dept" value="<?php echo $totalDept; ?>" />
                                </div>
                            </div>
                            
                            <div class="form-group">
                                <label class="col-lg-9 control-label"><?php echo $this->translate('totalpay'); ?></label>
                                <div class="col-lg-3">
                                    <input type="text" class="form-control" id="totalpay" value="0" />
                                    <input type="hidden" name="totalpay" value="0" />
                                </div>
                            </div>
                            
                            <div class="form-group">
                                <label class="col-lg-9 control-label"><?php echo $this->translate('remaining'); ?></label>
                                <div class="col-lg-3">
                                    <input readonly="" type="text" class="form-control" id="remaining" value="0" />
                                    <input type="hidden" name="remaining" value="0" />
                                </div>
                            </div> 
                            
                            <div class="form-group">
                                <label class="col-lg-9 control-label"><?php echo $this->translate('Số thùng'); ?></label>
                                <div class="col-lg-3">
                                    <input type="text" class="form-control" id="number" value="0" />
                                    <input type="hidden" name="number" value="0" />
                                </div>
                            </div> 
                            
                            <div class="form-group">
                                <label class="col-lg-4 control-label">&nbsp;</label>
                                <div class="col-lg-4">
                                    <div id="form-actions" class="form-actions">
                                        <button id="submit" onclick="submit()" type="submit" class="btn btn-primary">
                                            <i class="icon-save"></i>
                                            <?php echo $this->translate('save'); ?>
                                        </button>
                                        <button id="cancel" type="button" class="btn">
                                            <i class="icon-cancel-circle-2"></i>
                                            <?php echo $this->translate('cancel'); ?>
                                        </button>
                                    </div>
                                </div>
                            </div> 
                            <input type="hidden" name="supplier_id" value="<?php echo $this->supplier_id; ?>" />
                            <input type="hidden" name="save" value="1" />
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<div id="goSubmit">
<a href="#form-actions" class="tip-top" title="Go Top">
    <i class="icon-arrow-down"></i>
</a>
</div>
<script>
var optionNumeric1 = {'aSep': '.', 'aDec': ',', 'mDec': 0, 'vMin': 0, 'vMax': 99999999999, 'wEmpty': 'zero'};
var optionNumeric2 = {'aSep': '.', 'aDec': ',', 'mDec': 0, 'vMin': -99999999999, 'vMax': 99999999999};

function updatePrice(id){
    var price = prompt("Nhập giá sản phẩm");
    
    if(isNaN(price) || price.length == 0){
        alert("Giá tiền chỉ được nhập số");
        return;
    }else{
        $.ajax({
            url: "<?php echo $this->url(array('controller' => 'buy-invoice', 'action' => 'update-price')) ?>",
            data: {'pid': id, 'price': price},
            success: function(e){
                if(e == "true"){
                    $("label[id='text_price[" + id + "][price]']").autoNumeric('init', optionNumeric1);
                    $("label[id='text_price[" + id + "][price]']").autoNumeric('set', price);
                    $("input[name='product[" + id + "][price]']").val(price);
                    total(id);
                }else{
                    alert("Lỗi");
                }
            }
        });
    }
}

function total(id){
    var price = $("input[name='product[" + id + "][price]']").val();
    var quantity = $("input[name='product[" + id + "][quantity]']").val();
    var total = parseInt(price) * parseInt(quantity);
    
    $("input[name='product[" + id + "][total]']").val(total);
    $("input[id='product[" + id + "][total]']").val(total);
    subtotal();
    formatCurrency("input[id='product[" + id + "][total]']");
}

function subtotal(){
    var inputTotal = $("input#total").toArray();
    var subtotal = 0;
    
    for(var i = 0; i < inputTotal.length; i ++){
        var t = parseInt(inputTotal[i].value);
        subtotal += t;
    }
    
    $("input[name='subtotal']").val(subtotal);
    
    $("input#subtotal").autoNumeric('init', optionNumeric1);
    $("#subtotal").autoNumeric('set', subtotal);
    
    remaining();
}

function formatCurrency(selector){
    $(selector).priceFormat({
        prefix: '',
        clearPrefix: true,
        thousandsSeparator: '.',
        centsLimit: 0
    });
}

function remaining(){
    var subtotal = $("input[name='subtotal']").val();
    var dept = $("input[name='dept']").val();
    var totalpay = $("input[name='totalpay']").val();
    
    var remaining = (parseInt(subtotal) + parseInt(dept)) - parseInt(totalpay);
    
    $("input[name='remaining']").val(remaining);
    
    $("input#remaining").autoNumeric('init', optionNumeric2);
    $("input[id='remaining']").autoNumeric('set', remaining);
}

$("#cancel").click(function(){
    window.location = "<?php echo $this->url(array('module' => 'admin', 'controller' => 'buy-invoice', 'action' => 'manage'), null, true); ?>";
});

function submit(){
    var arrayList = $('input#quantity').get();
    
    $.each(arrayList, function(){
        var q = $(this).val();
        
        if(q == 0){
            $(this).parent("div#quantity").parent("div#product").remove();
        }
    });
}

$(document).ready(function(){
    <?php
        $timeNow = null;
        if(isset($this->old_order)) 
            $timeNow = date("d/m/Y H:i", strtotime($this->old_order['order']['date']));
        else 
            $timeNow = date("d/m/Y H:i");
    ?>
    $( "#datepicker" ).datetimepicker({
        step: 30,
        format: 'd/m/Y H:i',
        mask: true,
        value: '<?php echo $timeNow; ?>',
        closeOnDateSelect: true
    });
    <?php if(isset($this->orderID) && $this->orderID > 0): ?>
        window.open('<?php echo $this->url(array('module' => 'admin', 'controller' => 'buy-invoice', 'action' => 'view', 'order_id' => $this->orderID), null, true); ?>', 'Chi tiết', 'scrollbars=1,width=1024,height=768');
        window.location.href = "<?php echo $this->url(array('module' => 'admin', 'controller' => 'buy-invoice', 'action' => 'manage'), null, true); ?>"; 
    <?php endif; ?>

    $("input#quantity").change(function(){
        var id = $(this).parent("div#quantity").parent("div#product").children("input#id").val();
        var quantity = $(this).autoNumeric('get');
        
        $("input[name='product[" + id + "][quantity]']").val(quantity);
        total(id);
    });
    
    $("input#totalpay").change(function(){
        var totalpay = $(this).autoNumeric('get');
        
        $("input[name='totalpay']").val(totalpay);
        remaining();
    });
    
    
    $("select").change(function(){
        var id = $(this).parent("div#discount_type").parent("div#product").children("input#id").val();
        
        total(id);
    });
    
    $("input#quantity, input#number, input#totalpay, input#subtotal").autoNumeric('init', optionNumeric1);
    $("input#dept").autoNumeric('init', optionNumeric2);
    $("input#dept").autoNumeric('set', <?php echo $totalDept; ?>);
    
    $("input#number").change(function(){
        var quantity = $(this).autoNumeric('get');
        $("input[name='number']").val(quantity);
    });
    
    $("#submit").click(function(){
        submit();
    });
    
    <?php
        if(isset($this->old_order)){
            foreach($this->old_order['products'] as $row){
                ?>
                $("input[name='product[" + <?php echo $row['product_id']; ?> + "][quantity]']").val(<?php echo $row['quantity']; ?>);
                $("input[name='product[" + <?php echo $row['product_id']; ?> + "][quantity]']").parent("div#quantity").children("#quantity").val(<?php echo $row['quantity']; ?>);
                
                total('<?php echo $row['product_id']; ?>');
                <?php
            }
            ?>
            $("input#number").autoNumeric('set', <?php echo $this->old_order['order']['number']; ?>);
            $("input[name='number']").val(<?php echo $this->old_order['order']['number']; ?>);
            
            $("input#totalpay").autoNumeric('set', <?php echo $this->old_order['order']['totalpay']; ?>);
            $("input[name='totalpay']").val(<?php echo $this->old_order['order']['totalpay']; ?>);
            remaining();
            <?php
        }
    ?>
    
    $("#datepicker").change(function(){
        var date = $(this).val();
        $.ajax({
            url: "<?php echo $this->url(array('module' => 'admin', 'controller' => 'buy-invoice', 'action' => 'get-dept-ajax'), null, true) ?>",
            data: {'date': date, 'supplier_id': <?php echo $this->supplier_id; ?> <?php if(isset($this->old_order)) echo ",'currOrder': " . $this->old_order['order']['id']; ?>},
            success: function(e){
                $("input#dept").autoNumeric('set', e);
                $("input[name='dept']").val(e);
                remaining();
            }
        });
    })
});

$("form#salesinvoice").submit(function(){
    $("#submit").focus();
});
</script>
<style>
#salesinvoice div#headtitle, #salesinvoice div#product{
    padding: 0 0 15px 0;
    border-bottom: 1px solid #DDDDDD;
}
div#goSubmit{
    position: fixed;
    bottom: 0px;
    right: 100px;
    padding: 0px 5px;
    background-color: #45484d;
}

div#goSubmit a{
    width: 40px;
    font-size: 30px;
    height: 30px;
    color: #fff;
}
</style>